//获得指定格式的日期
Date.prototype.format2 = function (fmt) { //author: meizz   
    "use strict";  
    var o = {  
        "M+": this.getMonth() + 1, //月份   
        "d+": this.getDate(), //日   
        "h+": this.getHours(), //小时   
        "m+": this.getMinutes(), //分   
        "s+": this.getSeconds(), //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds() //毫秒   
    };  
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
    for (var k in o)  
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
    return fmt;  
};  

//加载操作栏按钮
var regist_caozuo=function regist_caozuo(value,row,index){
	var btm="";
	var date= new Date().format2("yyyy-MM-dd");
	if(row.status=="未看诊"&& row.visit_time>=date){
		btm="<center><input type='button' class='gaihao eidt' onclick='change_reg("+"\""+row.patient_registration_id+"\""+",\""+row.doctor_id+"\""+ ")' value='改号'/></center>";
	}
	if(btm==""){
		btm="不可改号";
	}
	return btm;
};
//查看所有门诊挂号信息
function check_all_patient(){
	$("#hosp_patient").datagrid("load",{page:'1'});
}
//门诊时间段搜索
function check_patient_by_time(){
	var opration_start_time=$("#card_opration_time_start").combo('getText');
	var opration_end_time=$("#card_opration_time_end").combo('getText');
	var visit_start_time=$("#card_visit_time_start").combo('getText');
	var visit_end_time=$("#card_visit_time_end").combo('getText');
	
	var param="{page:'1'";
	var bool=false;
	if(opration_start_time!=""){
		param+=",begin_time:'"+opration_start_time+"'";
		bool=true;
	}if(opration_end_time!=""){
		param+=",end_time:'"+opration_end_time+"'";
		bool=true;
	}if(visit_start_time!=""){
		param+=",visit_start_time:'"+visit_start_time+"'";
		bool=true;
	}if(visit_end_time!=""){
		param+=",visit_end_time:'"+visit_end_time+"'";
		bool=true;
	}if(!bool){
		$.messager.alert("提示","请选择时间");
		return '';
	}
	param+="}";
	var param2= eval('(' + param + ')'); 
	$("#hosp_patient").datagrid("load",param2);
}
//门诊挂号混合查询
function check_patient_by_all_condition(){
	var opration_start_time=$("#card_opration_time_start").combo('getText');
	var opration_end_time=$("#card_opration_time_end").combo('getText');
	var visit_start_time=$("#card_visit_time_start").combo('getText');
	var visit_end_time=$("#card_visit_time_end").combo('getText');
	var condition=$("#patient_searchbox").searchbox("getName");
	var value=$("#patient_searchbox").searchbox("getValue");
	var pay_or_not=$("#patient_pay_status").combo("getText");
	var visit_status=$("#patient_visit_status").combo("getText");
	var sex=$("#patient_sex").combo("getText");
	var param="{page:'1'";
	if(opration_start_time!=""){
		param+=",begin_time:'"+opration_start_time+"'";
	}if(opration_end_time!=""){
		param+=",end_time:'"+opration_end_time+"'";
	}if(visit_start_time!=""){
		param+=",visit_start_time:'"+visit_start_time+"'";
	}if(visit_end_time!=""){
		param+=",visit_end_time:'"+visit_end_time+"'";
	}if(value!=""){
		param+=","+condition+":'"+value+"'";
	}if(visit_status!="就诊状态"){
		param+=",visit_status:'"+visit_status+"'";
	}if(sex!="病人性别"){
		param+=",sex:'"+sex+"'";
	}if(pay_or_not !="缴费类型"){
		param+=",pay_or_not:'"+pay_or_not+"'";
	}
	param+="}";
	var param2= eval('(' + param + ')'); 
	$("#hosp_patient").datagrid("load",param2);
}
//门诊搜索条件栏按下回车键
function load_registration_by_condition(){
	if($("#patient_searchbox").searchbox('isValid')){
		var condition=$("#patient_searchbox").searchbox("getName");
		var value=$("#patient_searchbox").searchbox("getValue");
		var parm="{"+condition+":'"+value+"'}";
		var param2= eval('(' + parm + ')'); 
		if(value!=""){
			$("#hosp_patient").datagrid("load",param2);
		}else{
			$.messager.alert("搜索提示","请输入搜索条件");
		}
	}
}

//急诊改号
function change_patient_regist2(card_id){
	
};
//查看所有急诊挂号信息
function check_all_patient2(){
	$("#hosp_patient2").datagrid("load",{page:'1'});
}
//急诊时间段搜索
function check_patient_by_time2(){
	var opration_start_time=$("#card_opration_time_start2").combo('getText');
	var opration_end_time=$("#card_opration_time_end2").combo('getText');
	var visit_start_time=$("#card_visit_time_start2").combo('getText');
	var visit_end_time=$("#card_visit_time_end2").combo('getText');
	var param="{page:'1'";
	var bool=false;
	if(opration_start_time!=""){
		param+=",begin_time:'"+opration_start_time+"'";
		bool=true;
	}if(opration_end_time!=""){
		param+=",end_time:'"+opration_end_time+"'";
		bool=true;
	}if(visit_start_time!=""){
		param+=",visit_start_time:'"+visit_start_time+"'";
		bool=true;
	}if(visit_end_time!=""){
		param+=",visit_end_time:'"+visit_end_time+"'";
		bool=true;
	}if(!bool){
		$.messager.alert("提示","请选择时间");
		return '';
	}
	param+="}";
	var param2= eval('(' + param + ')'); 
	$("#hosp_patient2").datagrid("load",param2);
}
//急诊挂号混合查询
function check_patient_by_all_condition2(){
	var opration_start_time=$("#card_opration_time_start2").combo('getText');
	var opration_end_time=$("#card_opration_time_end2").combo('getText');
	var visit_start_time=$("#card_visit_time_start2").combo('getText');
	var visit_end_time=$("#card_visit_time_end2").combo('getText');
	var condition=$("#patient_searchbox2").searchbox("getName");
	var value=$("#patient_searchbox2").searchbox("getValue");
	var pay_or_not=$("#patient_pay_status2").combo("getText");
	var visit_status=$("#patient_visit_status2").combo("getText");
	var sex=$("#patient_sex2").combo("getText");
	var param="{page:'1'";
	if(opration_start_time!=""){
		param+=",begin_time:'"+opration_start_time+"'";
	}if(opration_end_time!=""){
		param+=",end_time:'"+opration_end_time+"'";
	}if(visit_start_time!=""){
		param+=",visit_start_time:'"+visit_start_time+"'";
	}if(visit_end_time!=""){
		param+=",visit_end_time:'"+visit_end_time+"'";
	}if(value!=""){
		param+=","+condition+":'"+value+"'";
	}if(visit_status!="就诊状态"){
		param+=",visit_status:'"+visit_status+"'";
	}if(sex!="病人性别"){
		param+=",sex:'"+sex+"'";
	}if(pay_or_not !="缴费类型"){
		param+=",pay_or_not:'"+pay_or_not+"'";
	}
	param+="}";
	var param2= eval('(' + param + ')'); 
	$("#hosp_patient2").datagrid("load",param2);
}

//急诊搜索条件栏按下回车键
function load_registration_by_condition2(){
	if($("#patient_searchbox2").searchbox('isValid')){
		var condition=$("#patient_searchbox2").searchbox("getName");
		var value=$("#patient_searchbox2").searchbox("getValue");
		var parm="{"+condition+":'"+value+"'}";
		var param2= eval('(' + parm + ')'); 
		if(value!=""){
			$("#hosp_patient2").datagrid("load",param2);
		}else{
			$.messager.alert("搜索提示","请输入搜索条件");
		}
	}
}


var small_office_id="";
var small_office_name="";
var select_doctor_riqi=new Date().format2("yyyy-MM-dd");
var select_doctor_banci="上午";
//选择小科室时
function select_small_office(id,name){
	small_office_name=name;
	small_office_id=id;
	var param_select_doctor="to_doctor_select?small_office_id="+small_office_id+"&date="+select_doctor_riqi+"&classes="+select_doctor_banci;
	$("#menzhen_select_doctor").panel("refresh",param_select_doctor);
}
//选择日期时
function select_riqi(title,index){
	if(small_office_id!=""){
		select_doctor_riqi=title;
		var param_select_doctor="to_doctor_select?small_office_id="+small_office_id+"&date="+select_doctor_riqi+"&classes="+select_doctor_banci;
		$("#menzhen_select_doctor").panel("refresh",param_select_doctor);
	}
}
//选择班次时
function select_banci(title,index){
	if(small_office_id!=""){
		select_doctor_banci=title;
		var param_select_doctor="to_doctor_select?small_office_id="+small_office_id+"&date="+select_doctor_riqi+"&classes="+select_doctor_banci;
		$("#menzhen_select_doctor").panel("refresh",param_select_doctor);
	}
}

//加载挂号按钮
function select_doctor_caozuo(value,row,index){
	var btn="<center><input type='button' class='gaihao eidt' onclick='guahao(\""+row.staff_id+"\",\""+ row.doctor_name +"\",\""+row.doctor_title_name+"\")' value='挂号'/></center>";
	return btn;
}
var staff_id="";
var doctor_name="";
//点击挂号按钮
function guahao(staff_id1,doctor_name1,doctor_title_name1){
	staff_id=staff_id1;
	doctor_name=doctor_name1;
	var parm="to_charge_guahao?doctor_title_name="+doctor_title_name1;
	$("#to_charge_guahao").panel('refresh',parm);
	$("#to_charge_guahao").panel('open');
}
//给挂号收费赋值
function fuzhi2(){
	$("#charge_guahao_doctor_id").textbox('setValue',staff_id);
	$("#charge_guahao_doctor_name").textbox("setValue",doctor_name);
	$("#charge_guahao_small_office").textbox("setValue",small_office_name);
	$("#charge_guahao_riqi").textbox("setValue",select_doctor_riqi);
	$("#charge_guahao_banci").textbox("setValue",select_doctor_banci);
	
}
//缴费
function charge_commit(){
	$("#charge_guahao").form({
		url:'verify_card_and_charge',
		success: function(v){
			$.messager.alert("挂号结果",v.substring(1,v.length-1));
			if(v.trim()=="\"该诊疗卡不存在！请重新输入\""){
			}else{
				$("#to_charge_guahao").panel("close",true);
			}
		}
	});
	$("#charge_guahao").submit();
	
}

//门诊选择搜索条件
function on_select_menu2(item){
	var select_;
	var valid;
	if(item.name=="registration_number"){
		select_=$("#reg_number");
		valid="card_id";
	}else if(item.name=="patient_name"){
		select_=$("#reg_patient_name");
		valid="search_name";
	}else if(item.name=="age"){
		select_=$("#reg_age");
		valid="age";
	}else if(item.name=="cradId"){
		select_=$("#reg_cradId");
		valid="card_id";
	}else if(item.name=="idCard"){
		select_=$("#reg_idCard");
		valid="id_card";
	}else if(item.name=="doctor_name"){
		select_=$("#reg_doctor_name");
		valid="search_name";
	}
	$("#reg_condition").children().each(function(){
		if($(this)!=select_){
			var option=$(this).attr("name");
			$(this).attr("data-options","name:'"+option+"'");
		}
	});
	var a=select_.attr("data-options")+",selected:true";
	select_.attr("data-options",a);
	$("#patient_searchbox").searchbox("selectName",item.name);
	$("#patient_searchbox").searchbox({
		validType:valid
	})
}
//急诊选择搜索条件
function on_select_menu3(item){
	var select_;
	var valid;
	if(item.name=="registration_number"){
		select_=$("#reg_number2");
		valid="card_id";
	}else if(item.name=="patient_name"){
		select_=$("#reg_patient_name2");
		valid="search_name";
	}else if(item.name=="age"){
		select_=$("#reg_age");
		valid="age";
	}else if(item.name=="cradId"){
		select_=$("#reg_cradId2");
		valid="card_id";
	}else if(item.name=="idCard"){
		select_=$("#reg_idCard2");
		valid="id_card";
	}else if(item.name=="doctor_name"){
		select_=$("#reg_doctor_name2");
		valid="search_name";
	}
	$("#reg_condition2").children().each(function(){
		if($(this)!=select_){
			var option=$(this).attr("name");
			$(this).attr("data-options","name:'"+option+"'");
		}
	});
	var a=select_.attr("data-options")+",selected:true";
	select_.attr("data-options",a);
	$("#patient_searchbox2").searchbox("selectName",item.name);
	$("#patient_searchbox2").searchbox({
		validType:valid,
	})
	
}
//门诊改号
var reg_id;
var doctor_id;
function change_reg(reg_id2,doctor_id2){
	reg_id=reg_id2;
	doctor_id=doctor_id2;
	$("#change_reg_detail").window("open");
}
//选择科室大类
function on_select_big_office(record){    
    var url = 'get_small_office?big_office_id='+record.bigOfficeId;  
    $('#small_office').combobox('reload', url);
    $('#small_office').combobox({
    	value:"请选择科室小类"
    });
}
//选择科室小类
function on_select_small_office(record){
	var date=$("#seven_date").combobox("getText");
	var classes=$("#classes_select").combobox("getText");
	if(date!="请选择日期"&&classes!="请选择班次"){
		loadDoctor();
	}
}
//选择日期
function on_select_date(record){
	var text=$("#small_office").combobox("getText");
	var classes=$("#classes_select").combobox("getText");
	if(text!="请选择科室小类"&&text!="科室小类"&&classes!="请选择班次"){
		loadDoctor();
	}
}
//选择班次时
function onSelectClasses(record){
	var text=$("#small_office").combobox("getText");
	var date=$("#seven_date").combobox("getText"); 
	if(text!="请选择科室小类"&&text!="科室小类"&&date!="请选择日期"){
		loadDoctor();
	}
}
//加载医生信息
function loadDoctor(){
	var url='get_doctor_arrange?small_office_id='+$("#small_office").combobox("getValue")+
	"&data_="+$("#seven_date").combobox("getText")+
	"&classes="+$("#classes_select").combobox("getValue");
	$("#select_doctor_detail").combogrid({
		url:url
	})
}

function saveChangeReg(){
	console.log(doctor_id);
	console.log(reg_id);
	$("#changeRegForm").form({
		url:'checkRegMoney?oldId='+doctor_id,
		success: function(v){
			changeReg(v)
		}	
	});
	$("#changeRegForm").submit();
}
//确认改号
function changeReg(v){
	$.messager.confirm("挂号提示",v.substring(1,v.length-1)+"，点击确定改号",function(r){
		if(r){
			$("#changeRegForm").form({
				url:'changeReg?reg_id='+reg_id,
				success: function(v){
					$.messager.alert("改号结果",v.substring(1,v.length-1));
					$("#hosp_patient").datagrid("reload");
				}
			});
			$("#changeRegForm").submit();
		}
	});
}
